package org.jeecg.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.message.MessageSend;
import org.jeecg.common.util.FastJsonRedisSerializer;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.shiro.authc.util.JwtUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.model.SysLoginModel;
import org.jeecg.modules.system.model.SysUserMobile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author zxh
 * @Title: AccountController
 * @ProjectName jeecg-boot
 * @Description: TODO
 * @date 2019/3/815:52
 */
@RestController
@RequestMapping("/account")
@Api("用户注册以及通过手机号登录")
public class AccountController {
    @Autowired
    private MessageSend messageSend;
    //短信过期时间 分钟
    @Value(value = "${message.deadline}")
    public String deadline;

    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping(value = "/sms", method = RequestMethod.POST)
    @ApiOperation("注册时候获取短信")
    public Result<SysUserMobile> sms(@RequestBody SysUserMobile sysUserMobile) {

        Result<SysUserMobile> result = new Result<SysUserMobile>();
        String verifyCode = String
                .valueOf(new Random().nextInt(899999) + 100000);//生成短信验证码
        String []phones={sysUserMobile.getMobile()};
        String []params={verifyCode,deadline};
        sysUserMobile.setResult(messageSend.sendMessage(params,phones));//调用腾讯云 发送短信
        sysUserMobile.setRedisKey("mobile"+sysUserMobile.getMobile());//redis key
        sysUserMobile.setVerifyCode(verifyCode);//redis value
        FastJsonRedisSerializer fastJsonRedisSerializer=new FastJsonRedisSerializer(SysUserMobile.class);
        //保存验证码到Redis 过期时间2分钟
        redisUtil.set(sysUserMobile.getRedisKey(),fastJsonRedisSerializer.serialize(sysUserMobile),Integer.parseInt(deadline)*3600);

        result.setResult(sysUserMobile);
        result.success("发送成功");
        return result;
    }
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ApiOperation("注册")
    public Result<SysUserMobile> register(@RequestBody SysUserMobile sysUserMobile) {

        Result<SysUserMobile> result = new Result<SysUserMobile>();


        result.setResult(sysUserMobile);
        result.success("登录成功");
        return result;
    }


}
